
import { getUserUseCourse, pauseCourseApi, startCourseApi, updateUserCourseStatus } from '~/api/deviceApi';

// 暂停疗程
const pauseCourse = async ({ user_course_id, device_sn, mobile, type, course_id = '' }) => {
    try {
        const response = await pauseCourseApi({ user_course_id, device_sn, mobile, type, course_id });
        return response;
    } catch (error) {
        throw error;
    }
};

// 开启疗程
const startCourse = async ({ user_course_id, device_sn, mobile, type, }) => {
    try {
        const response = await startCourseApi({ user_course_id, device_sn, mobile, type, });
        return response;
    } catch (error) {
        throw error;
    }
};
// 切换疗程
const switchCourse = async ({ user_course_id, device_sn, mobile, type, course_id = '' }) => {
    try {
        const response = await updateUserCourseStatus({ user_course_id, device_sn, mobile, type, course_id });
        return response;
    } catch (error) {
        throw error;
    }
};

// 查询并获取对应疗程参数
const getUserCourse = async (user_course_id, device_sn, mobile = '') => {
    try {
        const result = await getUserUseCourse({ user_course_id, device_sn, mobile });
        if (result.status !== 1) {
            Toast.show({ content: result.msg, layout: "top" });
            return { status: result.status };
        }
        const parametersArray = result.parameter_list.map(v => ({
            power: parseInt(v.power_level),
            duration: parseInt(v.start_duration),
            startHour: parseInt(v.start_time.split(":")[0]),
            startMinute: parseInt(v.start_time.split(":")[1]),
        }));
        const {
            course_type_sn,
            course_cycle_work_days,
            course_cycle_rest_days
        } = result.course_data;
        const laserParams = {
            index: parseInt(course_type_sn),
            periodic: parseInt(course_cycle_work_days),
            gap: parseInt(course_cycle_rest_days),
            endDate: parseInt(result.remaining_days),
            parameters: parametersArray
        };

        return { status: result.status, laserParams, msg: result.msg };
    } catch (error) {
        console.log("获取疗程失败error", error);
        throw error;
    }
};

export { startCourse, pauseCourse, getUserCourse, switchCourse }